package com.bii.gelapp;

import java.io.ByteArrayOutputStream;
import java.util.ArrayList;

import org.opencv.core.Mat;
import org.opencv.core.Scalar;

import android.R.color;
import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

public class MarkerFullscreenActivity extends Activity {
	boolean editmode = false;
	boolean erasemode = false;
	boolean valuemode = false;

	public Bitmap originalBmp; // origin
	public static Bitmap activeBmp; // edited image
	public static Bitmap takenBmp;

	public static ArrayList<ConnectedComponent> markerlist = new ArrayList<ConnectedComponent>();
	//to store colors
	public static ArrayList<int[]>colorlist = new ArrayList<int[]>();

	// menu items
	public Menu menu;

	public Bitmap getOriginalBmp() {
		return originalBmp;
	}

	public void setOriginalBmp(Bitmap bmp) {
		originalBmp = bmp;
	}

	public MarkerTouchImageView imgView;

	public static ArrayList<ConnectedComponent> getMarkerlist() {
		return markerlist;
	}
	public static ArrayList<int[]> getColorlist() {
		return colorlist;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.markerfullscreenactivity);
		
		if (getIntent().hasExtra("markerlist")) {
			markerlist = (ArrayList<ConnectedComponent>) getIntent()
					.getSerializableExtra("markerlist");
		}

		byte[] byteArray = getIntent().getByteArrayExtra("BMP");
		originalBmp = BitmapFactory.decodeByteArray(byteArray, 0,
				byteArray.length);

		imgView = new MarkerTouchImageView(this, originalBmp);

		imgView.setMaxZoom(4f);

		setContentView(imgView);
		
		// instructions
		String donotshow = "";
		final Dialog instructiondialog = new Dialog(MarkerFullscreenActivity.this);
		instructiondialog.setContentView(R.layout.instructiondialog);
		instructiondialog.setTitle("Attention");
		instructiondialog.setCancelable(true);
		instructiondialog.setCancelable(false);

		TextView tvInstructions = (TextView) instructiondialog
				.findViewById(R.id.tvInstructions);
		ImageView imgView1 = (ImageView) instructiondialog
				.findViewById(R.id.ivInstructions);
		final CheckBox donotshowbox = (CheckBox) instructiondialog.findViewById(R.id.donotshow);
		Button btOk = (Button) instructiondialog.findViewById(R.id.btOk);

		final SharedPreferences donotshowinstructions = this.getSharedPreferences(
				"instructions", 0);
		donotshow = donotshowinstructions.getString("markermarkereditinstructions",
				"nothing");
		if (donotshow.equals("nothing")) {
			donotshow = "Show";
		}

		if (donotshow.equals("Show")) {
			tvInstructions.setText("If detection of fragments are incorrect, redraw the rectangles, "
					+ "make sure the fragment is within the drawn rectangle."
					+ "\nclick save changes to continue, discard changes to return"
					);
			
			Drawable drawable = getResources().getDrawable(R.drawable.markermarkeredit_instruction);

			Bitmap instructionbmp = ((BitmapDrawable)drawable).getBitmap();
			
			imgView1.setImageBitmap(instructionbmp);
			
			instructiondialog.show();

			btOk.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					if(donotshowbox.isChecked()){
						SharedPreferences.Editor editor = donotshowinstructions.edit();
						editor.putString("markermarkereditinstructions", "DoNotShow");
						editor.commit();
					}
					else
					{
						SharedPreferences.Editor editor = donotshowinstructions.edit();
						editor.putString("markermarkereditinstructions", "Show");
						editor.commit();
					}
					instructiondialog.dismiss();
				}
			});
			
		}
		//
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu1) {
		// TODO Auto-generated method stub
		this.menu = menu1;
		getMenuInflater().inflate(R.menu.markereditmenu, menu1);

		return true;
	}

	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		super.onOptionsItemSelected(item);
		switch (item.getItemId()) {
		case R.id.btEdit:
			if (editmode == false) {
				editmode = true;
				// change to alternate
				item.setTitle("Stop");
				imgView.setEditMode(editmode);

				// when editing all other functions to false
				// erase
				MenuItem EraseBut = menu.findItem(R.id.btErase);
				EraseBut.setTitle("erase");
				erasemode = false;
				imgView.setEraseMode(erasemode);
				//
				
			} else {
				// if click on stop, stop editing
				item.setTitle("edit markers");
				editmode = false;
				imgView.setEditMode(editmode);

			}
			break;
		case R.id.btConfirm:
			imgView.confirmation();
			Intent returnActivity = new Intent(MarkerFullscreenActivity.this,
					MarkerValueActivity.class);
			//byte[] bytes;
			//bytes = compressImage(activeBmp);
			//returnActivity.putExtra("activeBmp", bytes);
			if (markerlist.size() != 0) {
				returnActivity.putExtra("markerlist", markerlist);
			}
			if(colorlist.size() != 0){
				returnActivity.putExtra("colorlist", colorlist);
			}
			byte[] bytes1;
			bytes1 = compressImage(originalBmp);
			returnActivity.putExtra("BMP", bytes1);

			returnActivity.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			finish();
			startActivity(returnActivity);
			
			break;

		case R.id.btErase:
			if (erasemode == false) {
				erasemode = true;
				item.setTitle("Stop");
				imgView.setEraseMode(erasemode);
				// when editing all other functions to false
				// edit
				MenuItem EditBut = menu.findItem(R.id.btEdit);
				EditBut.setTitle("edit markers");
				editmode = false;
				imgView.setEditMode(editmode);
				//
				

			} else {
				// alternate
				item.setTitle("erase");
				erasemode = false;
				imgView.setEraseMode(erasemode);

			}
			break;
		case R.id.btCancel:
			Intent returnActivity2 = new Intent(MarkerFullscreenActivity.this,
					MarkerCompareActivity.class);
			
			byte[] bytesorg;
			bytesorg = compressImage(originalBmp);
			returnActivity2.putExtra("BMP", bytesorg);
			activeBmp = originalBmp;

			if (markerlist.size() != 0) {
				returnActivity2.putExtra("markerlist", markerlist);
			}

			startActivity(returnActivity2);
			break;

			
		}
		return true;
	}
	
	public byte[] compressImage(Bitmap image) {
		ByteArrayOutputStream stream = new ByteArrayOutputStream();
		image.compress(Bitmap.CompressFormat.PNG, 0, stream);
		byte[] bytes = stream.toByteArray();
		return bytes;
	}
}
